<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ImageContentDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/ImageContentDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class ImageContentDaoImpl implements ImageContentDao {

    public function createNewImageContent($imageID, $imagePath, $imageName, $descID, $imageCreatedDateTime) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        if (is_null($descID) || empty($descID) || $descID == "") {
            $stmt = $mysqli->prepare("INSERT INTO SWISS_IMAGE_CONTENT (IMAGE_ID,IMAGE_NAME,IMAGE_PATH,CREATED_DATE_TIME) VALUES (?,?,?,?)");
            $stmt->bind_param('ssss', $imageID, $imageName, $imagePath, $imageCreatedDateTime);
        } else {
            $stmt = $mysqli->prepare("INSERT INTO SWISS_IMAGE_CONTENT (IMAGE_ID,IMAGE_NAME,IMAGE_PATH,DESC_ID,CREATED_DATE_TIME) VALUES (?,?,?,?,?)");
            $stmt->bind_param('sssss', $imageID, $imageName, $imagePath, $descID, $imageCreatedDateTime);
        }
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteImageContentByID($imageID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_IMAGE_CONTENT WHERE IMAGE_NAME = ?");
        $stmt->bind_param('s', $imageID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getAllImageContentByDescID($descID) {
        require '../com.swiss.mooh.model/Image.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        if (is_null($descID) || empty($descID) || $descID == "") {
            $detailList = new ArrayList();
            $stmt = $mysqli->prepare("SELECT * FROM SWISS_IMAGE_CONTENT WHERE DESC_ID IS NULL;");
            $stmt->execute();
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                $imageVO = new Image();
                $imageVO->setImageID(null);
                $imageVO->setImageCreateDateTime($x['CREATED_DATE_TIME']);
                $imageVO->setImageID($x['IMAGE_ID']);
                $imageVO->setImagePath($x['IMAGE_PATH']);
                $imageVO->setImageName($x['IMAGE_NAME']);

                $detailList->Add($imageVO);
            }
            $stmt->close();
            return $detailList;
        } else {
            $detailList = new ArrayList();
            $stmt = $mysqli->prepare("SELECT * FROM SWISS_IMAGE_CONTENT WHERE DESC_ID LIKE ?;");
            $stmt->bind_param('s', $descID);
            $stmt->execute();
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                $imageVO = new Image();
                $imageVO->setImageID($x['DESC_ID']);
                $imageVO->setImageCreateDateTime($x['CREATED_DATE_TIME']);
                $imageVO->setImageID($x['IMAGE_ID']);
                $imageVO->setImagePath($x['IMAGE_PATH']);
                $imageVO->setImageName($x['IMAGE_NAME']);

                $detailList->Add($imageVO);
            }
            $stmt->close();
            return $detailList;
        }
    }

    public function getImageContentByID($imageID) {
        
    }

    public function updateImageContent($imageTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_IMAGE_CONTENT SET DESC_ID = ? WHERE DESC_ID IS NULL;");
        $stmt->bind_param('s', $imageTO);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteAllFileTemp() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_IMAGE_CONTENT WHERE DESC_ID IS NULL;");
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getImageByDescID($descID) {
        require '../com.swiss.mooh.model/Image.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_IMAGE_CONTENT WHERE DESC_ID LIKE ?;");
        $stmt->bind_param('s', $descID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $imageVO = new Image();
            $imageVO->setDescID($x['DESC_ID']);
            $imageVO->setImageCreateDateTime($x['CREATED_DATE_TIME']);
            $imageVO->setImageID($x['IMAGE_ID']);
            $imageVO->setImagePath($x['IMAGE_PATH']);
            $imageVO->setImageName($x['IMAGE_NAME']);

            $detailList->Add($imageVO);
        }
        $stmt->close();
        return $detailList;
    }

}
